package com.hnyyjsxy.dao.impl;

import com.hnyyjsxy.dao.StudentDao;
import com.hnyyjsxy.domain.ApplyFor;
import com.hnyyjsxy.domain.Student;
import com.hnyyjsxy.domain.Violate;
import com.hnyyjsxy.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @date 2021/02/04
 */
public class StudentDaoImpl implements StudentDao {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 通过学号查找学生信息
     * @param sid
     * @return
     */
    @Override
    public Student getStudentBySid(String sid) {
        Student student = null;
        //如果没有查找到会有异常
        try {
            String sql = "SELECT * FROM student WHERE sid=?";
            student = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Student>(Student.class), sid);
        }catch (Exception e){
        }
        return student;
    }

    /**
     * 通过学号查询该学生的违纪情况
     * @param sid
     * @return
     */
    @Override
    public List<Violate> queryDeregulation(String sid) {
        String sql = "SELECT * FROM violate WHERE sid=?";
        List<Violate> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Violate>(Violate.class), sid);
        return list;
    }

    /**
     * 学生申请调换宿舍
     * @param app
     */
    @Override
    public void studentApply(ApplyFor app) {
        String sql = "INSERT INTO apply_for(sid,old_building,old_room,old_bed,new_room,new_bed,judge) VALUE(?,?,?,?,?,?,0)";
        jdbcTemplate.update(sql,app.getSid(),app.getOld_building(),app.getOld_room(),app.getOld_bed(),app.getNew_room(),app.getNew_bed());

    }

    @Override
    public void alterCharge(Student student) {
        jdbcTemplate.update("UPDATE student set charge=? where building=? and room=? and bed=?" , student.getCharge(),student.getBuilding(),student.getRoom(),student.getBed());
    }
}
